package com.touchyourheart.model.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional;

public abstract class GenericService<T, ID extends java.io.Serializable, R extends JpaRepository<T, ID>> {
  
  @Autowired
  private R dao;

  public GenericService() {
  }

  public T findById(ID id) {
    return this.dao.findOne(id);
  }

  public Page<T> findAll(Pageable pageable) {
    return this.dao.findAll(pageable);
  }

  public long count() {
    return this.dao.count();
  }

  @Transactional
  public T save(T t) {
    return this.dao.saveAndFlush(t);
  }

  @Transactional
  public void delete(ID id) {
    this.dao.delete(id);
  }

  @Transactional
  public void delete(T t) {
    this.dao.delete(t);
  }

  protected R getDao() {
    return this.dao;
  }
}
